package blogsys.demo.server.impl;

import blogsys.demo.bean.PermissionInfo;
import blogsys.demo.bean.RoleInfo;
import blogsys.demo.bean.UserInfo;
import blogsys.demo.dao.*;
import blogsys.demo.dto.UserRolePermission;
import blogsys.demo.server.UserInfoServer;
import blogsys.demo.utils.JsonResult;
import blogsys.demo.utils.JwtUtils;
import blogsys.demo.utils.RedisUtiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static blogsys.demo.utils.ResultCode.*;
/**
 * 用户接口实现类
 */
@Service
public class UserInfoServerImpl implements UserInfoServer {
    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Autowired
    private RoleInfoDao roleInfoDao;

    @Autowired
    private PermissionInfoDao permissionInfoDao;


    /**
     * 用户登录
     * 验证用户
     * @param id
     * @param password
     * @return
     */
    @Override
    public JsonResult userLogin(String id, String password) {
        UserInfo userInfo = userInfoDao.selectById(id);

        if(id == null || userInfo == null || !userInfo.getUserInfoPassword().equals(password)){
            return new JsonResult(false,USER_CREDENTIALS_ERROR); //空账号 账号为空  密码不正确
        }

        //信息拉取
        List<RoleInfo> roleInfos = roleInfoDao.selectByUserId(id);



        List<String> userRole = new ArrayList<>();
        List<String> userPermission = new ArrayList<>();
        roleInfos.forEach((r)->{
            userRole.add(r.getRoleInfoName());
            List<PermissionInfo> permissionInfos = permissionInfoDao.selectByRoleId(r.getRoleInfoId());
            permissionInfos.forEach((p)->{
                userPermission.add(r.getRoleInfoName()+":"+p.getPermissionInfoName());
            });
        });

        System.out.println(userRole);
        System.out.println(userPermission);


        //认证成功 生成一个token 响应回去 将该信息用户权限存入redis中
        String jwtToken = JwtUtils.setJwtToken(id);

        //将信息写入redis中
//        String key = "user:" + id;
//        RedisUtiles.setCacheRedis(key,new UserRolePermission(id,password,userRole,userPermission));


        return new JsonResult(true,SUCCESS_login,jwtToken);
    }

    /**
     * 用户退出
     * 从redis中删除
     * @return
     */
    @Override
    public JsonResult userExit(HttpServletRequest request) {
        String id = JwtUtils.getIdByJwtToken(request);
        redisTemplate.delete("user:"+id);
        return new JsonResult(true,SUCCESS_logout);
    }

    /**
     * 用户注册
     * 先查询该账号是否存在
     *
     * 注册账号成功时 添加该账号的角色信息与权限信息
     * @param id
     * @param password
     * @return
     */
    @Override
    public JsonResult userRegister(String id, String password) {
        UserInfo userInfo = userInfoDao.selectById(id);
        if (userInfo != null){
            return new JsonResult(false,USER_Register_ERROR);
        }

        //添加用户
        userInfoDao.insertUser(id,password);

        //添加user用户  1为用户角色 2为admin
        userRoleDao.insertUserRole(id,"100001");
        return new JsonResult(true,SUCCESS_register);
    }
}
